package net.sx.minemodule.mvp.activities;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.dialog.ECAlertDialog;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineActivityMineMedalBinding;
import net.sx.minemodule.mvp.model.MineMedalInfoModel;
import net.sx.minemodule.mvp.presenters.MineMedalPresenter;
import net.sx.minemodule.mvp.views.MineMedalView;

/**
 * add shibo.zheng
 * 19/06/13
 * 我的奖牌 activity
 */

@Route(path = ARouterPath.PATH_MINE_MEDAL)
public class MineMedalActivity extends MyBaseFragmentActivity<MineActivityMineMedalBinding, MineMedalPresenter> implements MineMedalView {
    private String medalType;
    private RecyclerView recyclerView;  //兑换商品列表view
//    private MultiTypeAdapter adapter;   //兑换商品adapter

    @Override
    protected int getLayout() {
        return R.layout.mine_activity_mine_medal;
    }

    @Override
    protected void initEvent() {
        mPresenter.getMedalData();      //获取奖牌信息
        mPresenter.getConvertList();    //获取兑换商品信息

        recyclerView = mBinding.convertList;
//        adapter = new MultiTypeAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(mPresenter.getAdapter());

    }

    @Override
    protected MineMedalPresenter getPresenter() {
        return new MineMedalPresenter(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setTitle("我的奖牌");
        //奖牌明细点击事件
        mBinding.medalInfo.setOnClickListener(v -> {
            medalType = "medalInfo";
            ARouter.getInstance().build(ARouterPath.PATH_RECORD_INFO).withString("recordInfoType", medalType).navigation();
        });

        //常见问题item点击事件
        mBinding.commentProblem.setOnClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_COMMON_PROBLEM).navigation());

        //兑换说明按钮点击事件
        mBinding.convertExplain.setOnClickListener(v -> {
            ECAlertDialog dialog = ECAlertDialog.buildAlert(MineMedalActivity.this, R.string.mine_medal_explain_text, R.string.picture_cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            dialog.setTitle("");                //设置无标题
            dialog.setContentPadding(20, 20, 20, 20);
            dialog.setMesGravity(Gravity.LEFT); //设置message居左
            dialog.show();
        });

        //点击马上获得奖牌按钮
        mBinding.getConvert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ECAlertDialog dialog = ECAlertDialog.buildAlert(view.getContext(), "", "取消", (dialog1, which) -> {
                    //TODO 点击事件处理
                });
//        dialog.setContentView(R.layout.mine_dialog_layout); //自定义dialog布局文件
                //自定义dialog布局文件
                View inflate = LayoutInflater.from(view.getContext()).inflate(R.layout.mine_dialog_layout, null);
                Button btnConvert = inflate.findViewById(R.id.btn_convert);
                btnConvert.setOnClickListener(v -> {
                    Log.d("zhengshibo", "onClick: 兑换兑换");
                    ECAlertDialog convertDialog = ECAlertDialog.buildAlert(v.getContext(), "确认要用x积分兑换1个奖牌吗？", (dialog12, which) -> ToastUtil.showToast("确定兑换！！！"));
                    convertDialog.show();
                });
                Button btnBuy = inflate.findViewById(R.id.btn_buy);
                btnBuy.setOnClickListener(v -> ToastUtil.showToast("支付支付"));
                dialog.setContentView(inflate); //设置自定义布局进去
                dialog.setContentPadding(10, 20, 10, 20);  //设置边距
                dialog.setTitle("");        //设置无标题
                dialog.show();  //显示dialog
            }
        });

    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        //历史奖牌记录点击事件
        setTopRightButton("历史奖牌记录", 0, v -> {
            medalType = "medalHistory";
            ARouter.getInstance().build(ARouterPath.PATH_RECORD_INFO).withString("recordInfoType", medalType).navigation();
        });
    }

    @Override
    public void addMedalData(MineMedalInfoModel model) {
        mBinding.amount.setText(model.getMedalNum());               //设置奖牌数量
        mBinding.medalIngNum.setText(model.getMedalIngNum());       //设置正在打造的奖牌数量
        mBinding.totalMedalNum.setText(model.getTotalMedalNum());   //设置本月累计奖牌数量
    }
}
